package com.meichis.mcsappframework.camera;

import android.content.Context;
import android.view.OrientationEventListener;
import android.view.Surface;
import android.view.View;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by pei on 2016/7/15.
 */
public class MyOrientationEventListener extends OrientationEventListener{

    private int currentOrientation = 0;//当前屏幕方向
    private List<View> mViews = new ArrayList<>();

    public MyOrientationEventListener(Context context) {
        super(context);
        this.enable();
    }

    @Override
    public void onOrientationChanged(int orientation) {
        if (ORIENTATION_UNKNOWN == orientation) {
            return;
        }
        if ((315<orientation&&orientation<360)||(0<=orientation&&orientation<=45)){
            currentOrientation = Surface.ROTATION_0;
        }
        if (45<orientation&&orientation<=135){
            currentOrientation = Surface.ROTATION_90;
        }
        if (135<orientation&&orientation<=225){
            currentOrientation = Surface.ROTATION_180;
        }
        if (225<orientation&&orientation<=315){
            currentOrientation = Surface.ROTATION_270;
        }
        for (View view:mViews){
            view.setRotation(-currentOrientation*90);
        }
    }

    /**
     * 获取当前屏幕方向
     *  @return
     */
    public int getCurrentOrientation() {
        return currentOrientation;
    }

    /**
     * 设置视图随着方向旋转
     *  @param views
     */
    public void setViewsRotation(List<View> views){
        mViews = views;
    }
}
